﻿/*
创建人：zhunan
日期：2023.1.16
描述：自定义对象维护js
*/
var page = {
    pagetotal: 0,
    pageSize: 12,
    pageNumber: 1,
    mainUrl: getRootPath() + "/Ashx/CustomObjectMaint.ashx", //本页面后台路径
    //初始化页面
    init: function () {
        page.bind();
        page.initData(); //初始化页面数据
    },
    // 页面事件绑定
    bind: function () {
        // 批次信息
        $("#tbCustomObject").datagrid({
            columns: [[
        { field: 'customobjectid', title: '唯一id', hidden: true },
        { field: 'customobjectname', title: '分类对象名称', width: 120 },
        { field: 'customobjectdesc', title: '分类对象描述', width: 200 },
        { field: 'useqty', title: '子项数', width: 60 },
        { field: 'creater', title: '创建人', width: 80 },
        {
            field: 'createdate', title: '创建时间', width: 160,
            formatter: function (value, row, index) {
                if (value) {
                    return value.replace('T', ' ');
                } else {
                    return value;
                }
            }
        },
        { field: 'updater', title: '更新人', width: 80 },
        {
            field: 'lastchangedate', title: '更新时间', width: 160,
            formatter: function (value, row, index) {
                if (value) {
                    return value.replace('T', ' ');
                } else {
                    return value;
                }
            }
        },
        {
            field: 'isuse', title: '启用状态', width: 60,
            formatter: function (value, row, index) {
                if (value === 0) {
                    return '启用';
                } else {
                    return '停用';
                }
            },
            styler: function (value) {
                if (value === 1) {
                    return 'color:red;';
                } else {
                    return 'color:green;';
                }
            }
        }
        , { field: 'operate', title: '操作', width: 120, formatter: function (value, row, index) {
            var para = {
                customobjectid: row.customobjectid,
                customobjectname: row.customobjectname
            }

            var btnHtml = "<button   class='editItems' onclick=page.editItems('" + JSON.stringify(para) + "',false) style='cursor:pointer;width: 100px; height: 22px'>子项编辑</button>";
            return btnHtml
        }
        }
      ]],
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
                //按钮给样式
                $("#dataGridZone .editItems").linkbutton({ iconCls: 'icon-edit' });
            },
            //行点击事件
            onClickRow: function (rowIndex, rowData) {
				$("#txtInputCustomObjecId").textbox('setValue', rowData.customobjectid);
                $("#txtInputCustomObjectName").textbox('setValue', rowData.customobjectname);
                $("#txtInputCustomObjectDesc").textbox('setValue', rowData.customobjectdesc);
                $("#txtUseQty").textbox('setValue', rowData.useqty);
                $("#ddlInputIsuse").combobox('setValue', rowData.isuse);
            }
        });

        //批次表格分页
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize;
                page.pageNumber = pageNumber;
                page.search();
            }
        });

        //绑定分页go按钮
        $(function () {
            $('#pageGo').on("click", function () {
                var pageNumber = $(this).parents('tr').find('.pagination-num').val();
                page.pageNumber = pageNumber;
                page.search();
            });
        });

        // 查询
        $("#btnSearch").on("click", function () {
            page.pageNumber = 1;
            page.search();
        });

        // 重置按钮
        $('#btnReset').on("click", function () {
            page.resetData();
        });

        // 添加
        $("#btnSave").on("click", function () {
            page.insertData();
        });

        // 更新
        $("#btnUpdate").on("click", function () {
            page.updateData();
        });

        // 删除
        $("#btnDel").on("click", function () { 
            $.messager.confirm('提示', '你确定要删除吗?', function (r) {
                // 确认删除
                if (r) { page.deleteData(); }
            });
        });
    },
    editItems: function (params) {
        //console.log(params);
        var url = "CustomObjectItemsMaint2.html?v=" + new Date().getTime();
        url += "&parentDiv=dg";
        $('#dg').dialog({
            title: '子项数据查看编辑',
            fit: true,
            top: 10,
            closed: false,
            cache: false,
            modal: true,
            content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
            popupData: JSON.parse(params), //弹出页面的传参
            returnData: null,
            onClose: function () {

            }
        });
    },
    //初始化页面
    initData: function () { },
    // 查询
    search: function (pageNumber) {
        $('#tbCustomObject').datagrid('loadData', []);

        var createdateStart = "";
        if (!isNaN(dayjs($("#createdateStart").datebox('getValue')).$y)) {
            createdateStart = $("#createdateStart").datebox('getValue');
        }
        var createdateEnd = "";
        if (!isNaN(dayjs($("#createdateEnd").datebox('getValue')).$y)) {
            createdateEnd = $("#createdateEnd").datebox('getValue');
        }

        var formData = {
            action: "getData",
            pageSize: page.pageSize,
            pageNumber: pageNumber || page.pageNumber,
            customobjectname: $("#txtCustomObjectName").textbox('getValue'),
            creater: $("#txtCreateEmp").textbox('getValue'),
            createdateStart: createdateStart,
            createdateEnd: createdateEnd,
            isuse: $('#ddlIsuse').combobox('getValue')
        };

        $.ajax({
            type: "Post",
            url: page.mainUrl + "?actions=getData",
            data: formData,
            cashe: false,
            async: true,
            beforeSend: function () {
                $('#tbCustomObject').datagrid('loading');
                $(this).attr("disabled", "disabled"); //禁用按钮
            },
            success: function (data) {
                $('#tbCustomObject').datagrid('loaded');
                $(this).removeAttr("disabled"); //恢复按钮
                var result = JSON.parse(data);
                if (result.Result) {
                    //给分页对象赋值
                    page.pagetotal = result.RowCount;
                    $('#tbCustomObject').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
                    //console.log(result.DBTable);
                } else {
                    displayMessage(result.Message, false);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('#tbCustomObject').datagrid('loaded');
                $(this).removeAttr("disabled"); //恢复按钮
                alert(XMLHttpRequest.responseText);
            }
        });
    },
    // 添加
    insertData: function () {
        var customObjectName = $("#txtInputCustomObjectName").textbox('getValue').replace(/(^\s*)|(\s*$)/g, ""); //对象名称
        var customObjectDesc = $("#txtInputCustomObjectDesc").textbox('getValue').replace(/(^\s*)|(\s*$)/g, ""); //对象描述
        var isuse = $('#ddlInputIsuse').combobox('getValue'); //启用状态
        if (!customObjectName) {
            displayMessage("对象名称不能为空！", false);
            return;
        }
        $.ajax({
            type: "post",
            url: page.mainUrl + "?action=insertData",
            data: {
                action: "insertData",
                customObjectName: customObjectName,
                customObjectDesc: customObjectDesc,
                isuse: isuse
            },
            cashe: false,
            dataType: 'json',
            success: function (res) {
                if (res.Result) {
                    displayMessage('保存成功！', true);
                    $("#txtInputCustomObjecId").textbox('clear');
                    $("#txtInputCustomObjectName").textbox('clear');
                    $("#txtInputCustomObjectDesc").textbox('clear');
                    $('#ddlInputIsuse').combobox('select', 0);
                    page.search();
                } else {
                    displayMessage(res.Message, false);
                }
            }
        });
    },
    // 更新
    updateData: function () {
        var customObjectId = $("#txtInputCustomObjecId").textbox('getValue'); //对象唯一id
        var customObjectName = $("#txtInputCustomObjectName").textbox('getValue').replace(/(^\s*)|(\s*$)/g, ""); //对象名称
        var customObjectDesc = $("#txtInputCustomObjectDesc").textbox('getValue').replace(/(^\s*)|(\s*$)/g, ""); //对象描述
        var isuse = $('#ddlInputIsuse').combobox('getValue'); //启用状态
        if (!customObjectId) {
            displayMessage("请选择待更新的数据!", false);
            return;
        }
        $.ajax({
            type: "post",
            url: page.mainUrl + "?action=updateData",
            data: {
                action: "updateData",
                customObjectId: customObjectId,
                customObjectName: customObjectName,
                customObjectDesc: customObjectDesc,
                isuse: isuse
            },
            cashe: false,
            dataType: 'json',
            success: function (res) {
                if (res.Result) {
                    displayMessage('更新成功！', true);
                    $("#txtInputCustomObjecId").textbox('clear');
                    $("#txtInputCustomObjectName").textbox('clear');
                    $("#txtInputCustomObjectDesc").textbox('clear');
                    $('#ddlInputIsuse').combobox('select', 0);
                    page.search();
                } else {
                    displayMessage(res.Message, false);
                }
            }
        });
    },
    // 删除数据
    deleteData: function () {
        var selectRow = $("#tbCustomObject").datagrid('getSelected');
        if (selectRow == null) {
            displayMessage('请选择记录！', true);
            return;
        }
        if (parseInt(selectRow.useqty)!=0) {
            displayMessage("所选对象有子项，不能删除!", false);
            return;
        }
        var customObjectId = selectRow.customobjectid; //对象唯一id
        var customObjectName = selectRow.customobjectname; //对象名
        $.ajax({
            type: "post",
            url: page.mainUrl + "?action=deleteData",
            data: {
                action: "deleteData",
                customObjectId: customObjectId,
                customObjectName: customObjectName
            },
            cashe: false,
            dataType: 'json',
            success: function (res) {
                if (res.Result) {
                    displayMessage('删除成功！', true);
                    page.search();
                    $("#txtInputCustomObjecId").textbox('clear');
                    $("#txtInputCustomObjectName").textbox('clear');
                    $("#txtInputCustomObjectDesc").textbox('clear');
                    $('#ddlInputIsuse').combobox('select', 0);
                } else {
                    displayMessage(res.Message, false);
                }
            }
        });
    },
    // 清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();

        if (isresult) {
            clearEasyuiDataGrid("tbCustomObject", 1);
        }
    },
    // 重置
    resetData: function () {
        $('#txtCustomObjectName').textbox('clear');
        $("#txtCreateEmp").textbox('clear');
        $("#createdateStart").datebox('clear');
        $("#createdateEnd").datebox('clear');
        $('#ddlIsuse').combobox('select', '全部');

        $("#txtInputCustomObjecId").textbox('clear');
        $("#txtInputCustomObjectName").textbox('clear');
        $("#txtInputCustomObjectDesc").textbox('clear');
        $('#ddlInputIsuse').combobox('select', 0);

        $("#tbCustomObject").datagrid('loadData', { total: 0, rows: [] });
        page.pageNumber = 1;
        clearMessage();
    }
};
page.init();